/**
	version 0.6
*/
var jspkg = {
	//dom object
	dom: {
		body: null,
		
		id: function (id, obj) {
			if(obj) {
				obj.id = id;
			}
			return document.getElementById(id);
		},

		setBody: function (obj) {
			if(obj) {
				this.body = obj;
			} else {
				this.body = document.body;
			}
		},
		
		isValidNode: function (obj) {
			if ((obj.tagName !== undefined || obj.tagName !== "#TEXT")) {
				return true;
			}
			return false;
		},
		
		getElementsByClassName: function (class, container) {
			this.setBody();
			var elements = [];
			var childs = {};
			if(container) {
				childs = container.childNodes;
			} else {
				childs = this.body.childNodes;
			}
			
			for(var i = 0; i < childs.length; i++) {
				if(childs[i].className === class && this.isValidNode(childs[i])) {
					elements[elements.length] = childs[i];
				}
			}
			return elements;
		},

		getElementsByAttr: function (name, value) {
			this.setBody();
			var elements = [];
			var childs = this.body.childNodes;
			for(var i = 0; i < childs.length; i++) {
				if(this.isValidNode(childs[i])) {
					if(value) {
						if(childs[i].hasAttribute(name)&& childs[i][name] === value) {
							elements[elements.length] = childs[i];
						}
					} else {
						if(childs[i].hasAttribute(name)) {
							elements[elements.length] = childs[i];
						}
					}
				}
			}
			return elements;
		},
		
		getChildNodes: function (obj) {
			var elements = [];
			var nodes = obj.childNodes;
			for (var i = 0; i < nodes.length; i ++) {
				if (this.isValidNode(nodes[i])) {
					elements[elements.length] = nodes[i];
				}
			}
			return elements;
		},
		
		clear: function (obj) {
			if (obj.hasChildNodes() && this.isValidNode(obj)) {
				obj.innerHTML = "";
			}
		},
		
		getAttrs: function (obj) {
			var attrs = {};
			for(var i in obj) {
				if(obj.hasAttribute(i)) {
					attrs[i] = obj[i];
				}
			}
			return attrs;
		},
		
		insertNode: function (tag, container, options) {
			var obj = document.createElement(tag);
			if(options) {
				for(i in options) {
					obj[i] = options[i];
				}
			}
			container.appendChild(obj);
			return obj;
		},
		
		removeNode: function (obj) {
			if(this.isValidNode(obj)) {
				jspkg.event.removeAll(obj);
				document.removeChild(obj, obj.parentNode);
			}
			return obj.parentNode;
		},

		script: function (path) {
			this.setBody();
			var scriptTag = document.createElement("script");
			scriptTag.type = "text/javascript";
			scriptTag.src = path;
			this.body.appendChild(scriptTag);
			return scriptTag;
		},

		style: function (path, media) {
			this.setBody();
			var styleTag = document.createElement("link");
			styleTag.rel = "stylesheet";
			styleTag.media = media? media : "screen";
			styleTag.href = path;
			this.body.appendChild(styleTag);
			return styleTag;
		},

		addClass: function (obj, class) {
			obj.className = obj.className + " " + class;
		},

		removeClass: function (obj, class) {
			if(this.hasClass(obj,class)) {
				obj.className = obj.className.replace(class,"");
			}
		},

		hasClass: function (obj, class) {
			if(obj.className.indexOf(class) !== -1) {
				return true;
			}
			return false;
		},
		
		replaceClass: function (obj, class, newClass) {
			if(this.hasClass(obj,class)) {
				this.removeClass(obj, class);
			}
			this.addClass(obj, newClass);
		}
	},
	//css object
	css: {
		get: function (obj, prop) {
			
		}
	},
	//event object
	event: {
		stdEvent: window.addEventListener,
		ieEvent: window.event,
		buble: false,
		type: {
			"beforeunload" : "onbeforeunload ",
			"abort" : "onabort",
			"blur" : "onblur",
			"change" : "onchange",
			"click" : "onclick",
			"dblclick" : "ondblclick",
			"error" : "onerror",
			"focus" : "onfocus",
			"keydown" : "onkeydown",
			"keypress" : "onkeypress",
			"keyup" : "onkeyup",
			"load" : "onload",
			"mousedown" : "onmousedown",
			"mousemove" : "onmousemove",
			"mouseout" : "onmouseout",
			"mouseover" : "onmouseover",
			"mouseup" : "onmouseup",
			"move" : "onmove",
			"reset" : "onreset",
			"resize" : "onresize",
			"select" : "onselect",
			"submit" : "onsubmit",
			"unload" : "onunload"
		},
		
		checkEvent: function () {
			if(this.stdEvent) {
				return "mozilla"
			} else if(this.ieEvent) {
				return "ie";
			} else {
				return;
			}
		},
		
		add: function (obj, type, event) {
			if(this.checkEvent() === "mozilla") {
				obj.addEventListener(type, event, this.buble);
			} else if(this.checkEvent === "ie") {
				obj.attachEvent(this.type[type], event);
			} else {
				obj[this.type[type]] = event;
			}
		},
		
		remove: function (obj, type, event) {
			if(this.checkEvent() === "mozilla") {
				obj.removeEventListener(type, event, this.buble);
			} else if(this.checkEvent === "ie") {
				obj.dettachEvent("on" + type, event);
			} else {
				for(i in obj) {
					if(i === "on" + type) {
						if(obj["on" + type] === event) {
							delete obj["on" + type];
						}
					}
				}
			}
		},
		
		removeAll: function (obj) {
			for(i in this.type) {
				obj[this.type[i]] = null; 
			}
		}
	},
	//ajax object
	ajax: {
		method: "",
		url: "",
		params: null,
		type: "text/xml",
		xhr: {},
		async: true,
		
		dataType: { 
			"json" : "text/javascript",
			"html" : "text/html",
			"xml" : "text/xml",
			"text" : "text/plain"
		},
		
		cache: {
			"Pragma" : "no-cache",
			"Cache-Control" : "no-cache, no-store",
			"Expires" : "now"
		},
		
		meta: {
			"Content-type" : "application/x-www-form-urlencoded",
			"Connection" : "close"
		},
		
		state: {
			"unitialized" : 0,
			"starting" : 1,
			"loading" : 2,
			"preparing" : 3,
			"completed": 4
		},
		
		status: {
			"success" : 200,
			"notFound" : 404,
			"error" : 500
		},
		
		setXhr: function () {
			if(window.XMLHttpRequest) {
				this.xhr = new XMLHttpRequest();
			} else {
				try {
					this.xhr = new ActiveXObject("Msxml2.XMLHTTP");
				} catch(e) {
					new ActiveXObject("Microsoft.XMLHTTP");
				}
			}
		},
		
		start: function (callback) {
			var parent = this;
			this.setXhr();
			this.xhr.open(this.method, this.url, this.async);
			for (i in this.cache) {
				if(this.cache.hasOwnProperty(i)) {
					this.xhr.setRequestHeader(i, this.cache[i]);
				}
			}
			if(this.method === "POST") {
				for (i in this.meta) {
					if(this.meta.hasOwnProperty(i)) {
						this.xhr.setRequestHeader(i, this.meta[i]);
					}
				}
			} else {
				this.xhr.setRequestHeader("Content-type", this.dataType[this.type]);
			}
			this.xhr.onreadystatechange = function () {
				parent.response(callback);
			};
			this.xhr.send(this.params);
		},
		
		response: function (callback) {
			var data;
			if(this.xhr.readyState === this.state["completed"]) {
				if(this.xhr.status === this.status["success"]) {
					data = this.xhr.responseXML;
					(callback)(data);
				}
			}
		},
		
		get: function (url, callback) {
			this.method = "GET";
			this.url = url;
			this.start(callback);
		},
		
		post: function (url, params, callback) {
			this.method = "POST";
			this.url = url;
			this.params = params;
			this.start(callback);
		}
	}
};